import { join } from 'path';
import { Controller, Get, Header } from '@nestjs/common';
import * as fs from 'fs';
import * as path from 'path';
import XLSX from 'xlsx';
import { NotAuthorized } from 'src/decorator/roles.decorator';

@NotAuthorized()
@Controller('excel')
export class ExcelController {

    constructor() {
        // this.test()
    }

    @Get('test')
    @Header('content-type', 'application/octet-stream')
    test() {
        // const buffer =  fs.readFileSync('./public/test.xls')
        // var workbook = XLSX.read(buffer, {type: 'buffer'});

        var aoa = [
            ['姓名', '性别', '年龄', '注册时间'],
            ['张三', '男', 18, new Date()],
            ['李四', '女', 22, new Date()]
        ];
        var ws = XLSX.utils.aoa_to_sheet(aoa);
    
        var wb = XLSX.utils.book_new();
        XLSX.utils.book_append_sheet(wb, ws, "SheetJS");
        var buf = XLSX.write(wb, {type:'buffer', bookType:"xlsx"});
        return buf;
    }
}
